import {service} from '@/utils/request'

// 治疗管理API
export interface Treatment {
  id: string
  user_nickname: string
  device_id: string
  mode: string
  status: 'ongoing' | 'completed' | 'cancelled'
  start_time: string
  end_time: string | null
  duration: number
}

export interface TreatmentStats {
  totalTreatments: number
  completedTreatments: number
  completionRate: number
  avgDuration: number
}

export interface TreatmentListParams {
  page: number
  pageSize: number
  keyword?: string
  status?: string
  startTime?: string
  endTime?: string
}

export interface TreatmentListResponse {
  records: Treatment[]
  total: number
  page: number
  pageSize: number
}

export interface TreatmentTemplate {
  id: number
  template_name: string
  template_type: string
  treatment_mode: string
  treatment_duration: number
  current_intensity: number
  frequency: number
  is_default: boolean
  created_at: string
}

export interface TemplateListResponse {
  records: TreatmentTemplate[]
  total: number
  page: number
  pageSize: number
}

export interface TemplateListParams {
  page: number
  pageSize: number
  template_type?: string
  type?: string // 添加type参数：system 或 user
}

/**
 * 获取治疗记录列表
 */
export const getTreatmentList = (params: TreatmentListParams) => {
  return service<TreatmentListResponse>({
    url: '/treatments',
    method: 'get',
    params
  })
}

/**
 * 获取治疗详情
 */
export const getTreatmentDetail = (id: string) => {
  return service<Treatment>({
    url: `/treatments/${id}`,
    method: 'get'
  })
}

/**
 * 获取治疗统计数据
 */
export const getTreatmentStats = () => {
  return service<TreatmentStats>({
    url: '/treatments/statistics',
    method: 'get'
  })
}

/**
 * 获取治疗趋势数据
 */
export const getTreatmentTrends = (days: string) => {
  return service({
    url: '/treatments/statistics',
    method: 'get',
    params: { days }
  })
}

/**
 * 获取治疗模式分布
 */
export const getTreatmentModes = () => {
  return service({
    url: '/treatments/statistics',
    method: 'get'
  })
}

/**
 * 获取模板列表（支持系统模板和用户模板）
 */
export const getTemplateList = (params: TemplateListParams) => {
  return service<TemplateListResponse>({
    url: '/treatments/templates',
    method: 'get',
    params
  })
}

/**
 * 创建模板
 */
export const createTemplate = (data: Partial<TreatmentTemplate>) => {
  return service<TreatmentTemplate>({
    url: '/treatments/templates',
    method: 'post',
    data
  })
}

/**
 * 更新模板
 */
export const updateTemplate = (id: number, data: Partial<TreatmentTemplate>) => {
  return service<TreatmentTemplate>({
    url: `/treatments/templates/${id}`,
    method: 'put',
    data
  })
}

/**
 * 删除模板
 */
export const deleteTemplate = (id: number) => {
  return service({
    url: `/treatments/templates/${id}`,
    method: 'delete'
  })
}